package com.hxzy.servlet.discuss;

import com.hxzy.bean.Discuss;
import com.hxzy.bean.Message;
import com.hxzy.bean.User;
import com.hxzy.service.DiscussService;
import com.hxzy.service.impl.DiscussServiceImpl;
import com.hxzy.util.PageUtil;
import com.hxzy.vo.DiscussVo;
import com.hxzy.vo.JsonResult;
import com.hxzy.vo.StatusCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author nick
 */
@WebServlet("/discuss")
public class DiscussServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");


        //获取messageId
        String messageId_str = req.getParameter("messageId");

        if (null == messageId_str) {
            JsonResult jsonResult = new JsonResult();
            jsonResult.setCode(StatusCode.PARAMS_ERROR.getCode());
            jsonResult.setMessage(StatusCode.PARAMS_ERROR.getMessage());
            JsonResult.write2client(jsonResult,resp);
            return;
        }

        int messageId = Integer.parseInt(messageId_str);

        String pageIndexStr = req.getParameter("pageIndex");
        int pageIndex = PageUtil.DEFAULT_PAGE_INDEX;//默认第一页
        if (null != pageIndexStr) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }

        //通过业务逻辑层进行查询
        DiscussService service = new DiscussServiceImpl();
        PageUtil<DiscussVo> paging = service.paging(pageIndex, PageUtil.DEFAULT_PAGE_SIZE, messageId);

        //将结果响应给用户
        JsonResult.write2client(paging,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");

        String content = req.getParameter("content");
        String messageId_str = req.getParameter("messageId");
        if (null == content || null == messageId_str) {
            JsonResult jsonResult = new JsonResult();
            jsonResult.setCode(StatusCode.PARAMS_ERROR.getCode());
            jsonResult.setMessage(StatusCode.PARAMS_ERROR.getMessage());
            JsonResult.write2client(jsonResult,resp);
            return;
        }

        int messageId = Integer.parseInt(messageId_str);
        HttpSession session = req.getSession(false);
        Object object = session.getAttribute("user");
        if (null == object) {
            JsonResult jsonResult = new JsonResult();
            jsonResult.setCode(StatusCode.NOT_LOGIN.getCode());
            jsonResult.setMessage(StatusCode.NOT_LOGIN.getMessage());
            JsonResult.write2client(jsonResult,resp);
            return;
        }
        User user = (User) object;

        DiscussService service = new DiscussServiceImpl();
        Discuss discuss = new Discuss();
        discuss.setUser(user);
        discuss.setDate(LocalDateTime.now());
        discuss.setContent(content);

        Message message = new Message();
        message.setId(messageId);

        discuss.setMessage(message);
        int save = service.save(discuss);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(StatusCode.SUCCESS.getCode());
        jsonResult.setMessage(StatusCode.SUCCESS.getMessage());
        JsonResult.write2client(jsonResult,resp);
    }
}
